//@ts-nocheck
import * as THREE from 'three';
import { CustomSpriteMaterial } from './CustomSpriteMaterial';

export class CustomSprite extends THREE.Mesh {
  width: number;
  height: number;
  centerPosition: string;
  //默认用来处理的字体
  static readonly topLayout: string = 'top';
  static readonly rightLayout: string = 'right';
  static readonly buttomLayout: string = 'buttom';
  static readonly leftLayout: string = 'left';
  static readonly centerLayout: string = 'center';

  constructor(
    material: CustomSpriteMaterial,
    width: number,
    height: number,
    centerPosition: string = 'center',
  ) {
    super(new THREE.BufferGeometry(), material);
    this.width = width;
    this.height = height;
    this.centerPosition = centerPosition;
    this.name = 'sign';
    this.createWithCenterPosition(centerPosition);
    this.renderOrder = 900;
  }

  /**
   * 设置中心位置
   * @param centerX
   * @param centerY
   */
  private createWithCenterPosition(value: string) {
    let float32Array: Float32Array;
    switch (value) {
      case CustomSprite.topLayout:
        float32Array = new Float32Array([
          -this.width * 0.5,
          -this.height * 1.0,
          0,
          0,
          0,
          this.width * 0.5,
          -this.height * 1.0,
          0,
          1,
          0,
          this.width * 0.5,
          this.height * 0.0,
          0,
          1,
          1,
          -this.width * 0.5,
          this.height * 0.0,
          0,
          0,
          1,
        ]);
        this.createGeometry(float32Array);
        break;
      case CustomSprite.rightLayout:
        float32Array = new Float32Array([
          -this.width * 1.0,
          -this.height * 0.5,
          0,
          0,
          0,
          this.width * 1.0,
          -this.height * 0.5,
          0,
          1,
          0,
          this.width * 0.0,
          this.height * 0.5,
          0,
          1,
          1,
          -this.width * 0.0,
          this.height * 0.5,
          0,
          0,
          1,
        ]);
        this.createGeometry(float32Array);
        break;
      case CustomSprite.buttomLayout:
        float32Array = new Float32Array([
          -this.width * 0.5,
          -this.height * 0,
          0,
          0,
          0,
          this.width * 0.5,
          -this.height * 0,
          0,
          1,
          0,
          this.width * 0.5,
          this.height * 1.0,
          0,
          1,
          1,
          -this.width * 0.5,
          this.height * 1.0,
          0,
          0,
          1,
        ]);
        this.createGeometry(float32Array);
        break;
      case CustomSprite.leftLayout:
        float32Array = new Float32Array([
          -this.width * 0.0,
          -this.height * 0.5,
          0,
          0,
          0,
          this.width * 0.0,
          -this.height * 0.5,
          0,
          1,
          0,
          this.width * 1.0,
          this.height * 0.5,
          0,
          1,
          1,
          -this.width * 1.0,
          this.height * 0.5,
          0,
          0,
          1,
        ]);
        this.createGeometry(float32Array);
        break;
      case CustomSprite.centerLayout:
        float32Array = new Float32Array([
          -this.width * 0.5,
          -this.height * 0.5,
          0,
          0,
          0,
          this.width * 0.5,
          -this.height * 0.5,
          0,
          1,
          0,
          this.width * 0.5,
          this.height * 0.5,
          0,
          1,
          1,
          -this.width * 0.5,
          this.height * 0.5,
          0,
          0,
          1,
        ]);
        this.createGeometry(float32Array);
        break;
    }
  }

  /**
   * 创建geo
   */
  private createGeometry(float32Array: Float32Array): void {
    let interleavedBuffer = new THREE.InterleavedBuffer(float32Array, 5);
    let geometry = this.geometry as THREE.BufferGeometry;
    geometry.setIndex([0, 1, 2, 0, 2, 3]);
    geometry.setAttribute(
      'position',
      new THREE.InterleavedBufferAttribute(interleavedBuffer, 3, 0, false),
    );
    geometry.setAttribute(
      'uv',
      new THREE.InterleavedBufferAttribute(interleavedBuffer, 2, 3, false),
    );
  }

  public setPosition(pos: THREE.Vector3): void {
    this.position.x = pos.x;
    this.position.y = pos.y;
    this.position.z = pos.z;
  }

  dispose() {
    this.geometry.dispose();
    this.geometry = null;
    (this.material as CustomSpriteMaterial).texture.dispose();
    (this.material as CustomSpriteMaterial).texture = null;
    (this.material as CustomSpriteMaterial).dispose();
    this.material = null;
  }
}
